package org.kaindorf.kainflix.metadata.controller;

import org.kaindorf.kainflix.metadata.repository.entity.MetaData;
import org.kaindorf.kainflix.metadata.service.MetaDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/metadata")
public class MetaDataController {

    @Autowired
    private MetaDataService metaDataService;

    @RequestMapping(method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public String create(@RequestBody MetaData metaData) {
        return Long.toString(metaDataService.saveMetaData(metaData).getId());
    }

    @RequestMapping(value = "{metaDataId}", method = RequestMethod.PUT)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public void update(@PathVariable("metaDataId") long metaDataId, @RequestBody MetaData metaData) {
        metaData.setId(metaDataId);
        metaDataService.updateMetaData(metaData);
    }

    @RequestMapping(value = "{metaDataId}", method = RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public void delete(@PathVariable("metaDataId") long metaDataId) {
        metaDataService.delete(metaDataId);
    }

    @RequestMapping(value = "{metaDataId}", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public MetaData read(@PathVariable("metaDataId") long metaDataId) {
        return metaDataService.getMetaData(metaDataId);
    }

    @RequestMapping(method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public Iterable<MetaData> readAll() {
        return metaDataService.getAllMetaData();
    }
}
